package com.xm.xmapp;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.android.volley.VolleyError;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xm.xmapp.app.CodeConstant;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.app.Type;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.customview.CustomLayout;
import com.xm.xmapp.exception.MyException;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.request.bean.Get_order_info_by_idRequest;
import com.xm.xmapp.request.bean.Get_user_money_infoRequest;
import com.xm.xmapp.request.bean.Get_user_renewal_longrent_order_infoRequest;
import com.xm.xmapp.response.bean.BaseBeanResponse;
import com.xm.xmapp.response.bean.ResponseLongRentOrderInfo;
import com.xm.xmapp.response.bean.ResponseOrderInfoById;
import com.xm.xmapp.response.bean.ResponseUserMoney;
import com.xm.xmapp.response.bean.UserTimeBean;
import com.xm.xmapp.tools.Options;
import com.xm.xmapp.tools.StringTools;
import com.xm.xmapp.wxapi.WXPayEntryActivity;

@SuppressLint("SimpleDateFormat")
public class LongReRentOrderActivity extends BaseActivity implements OnClickListener {

	private TextView chexing, money, time, payMoney, pay, myinfo;
	private ImageView img;
	private CustomLayout returnTime, seclectTime, yajin, newReturnTime;
	private UserTimeBean bean = new UserTimeBean();
	private ResponseLongRentOrderInfo info;
	private boolean isOnclick = true;
	private String order_id;
	private ResponseOrderInfoById orderBean;
	double initMoney;

	/**
	 * 0续租支付成功后PayUitl调用 1是车辆押金充值后调用
	 */
	@Override
	public Object dataChang(int flag, Object data) {

		switch (flag) {
		case Type.A_LongReRentOrder.DEFAULT:
			finish();
			break;
		case Type.A_LongReRentOrder.PAY_CAR:
			getData(order_id);
			break;

		default:
			break;
		}
		return super.dataChang(flag, data);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_longrerent_order);
	}

	@Override
	public void initView() {

		chexing = (TextView) findViewById(R.id.chexing);
		pay = (TextView) findViewById(R.id.pay);
		img = (ImageView) findViewById(R.id.img);
		yajin = (CustomLayout) findViewById(R.id.yajin);
		money = (TextView) findViewById(R.id.money);
		time = (TextView) findViewById(R.id.time);
		payMoney = (TextView) findViewById(R.id.payMoney);
		myinfo = (TextView) findViewById(R.id.myinfo);

		returnTime = (CustomLayout) findViewById(R.id.returnTime);
		seclectTime = (CustomLayout) findViewById(R.id.seclectTime);
		newReturnTime = (CustomLayout) findViewById(R.id.newReturnTime);
		bean.time = 1;
		bean.name = 1 + "个月";
	}

	@Override
	public void initData() {

		setMyTitle("续用");
		order_id = getIntent().getStringExtra("order_id");
		pay.setBackgroundResource(R.drawable.myborder);
		getOrderData(order_id);

	}

	private void getOrderData(final String order_id) {

		promptDialog.show();
		Get_order_info_by_idRequest requestBean = new Get_order_info_by_idRequest();
		requestBean.order_id = order_id;
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {

				orderBean = JSON.parseObject(response, ResponseOrderInfoById.class);
				returnTime.setText2(StringTools.getLongTime(orderBean.info.long_rent_repay_date));
				chexing.setText(orderBean.info.car_number);
				ImageLoader.getInstance().displayImage(orderBean.info.car_genre_img, img,
						Options.getListOptions(R.mipmap.moren));
				getData(order_id);

			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
			}
		});

	}

	private void getData(String order_id) {

		Get_user_renewal_longrent_order_infoRequest requestBean = new Get_user_renewal_longrent_order_infoRequest();
		requestBean.order_id = order_id;
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {

				promptDialog.dismiss();
				BaseBeanResponse<String> tempBean = JSON.parseObject(response,
						new TypeReference<BaseBeanResponse<String>>() {
						});

				switch (tempBean.errCode) {
				case 0:
					info = JSON.parseObject(tempBean.data, ResponseLongRentOrderInfo.class);
					setSeclectTime();
					break;

				default:
					findViewById(R.id.bottom).setVisibility(View.VISIBLE);
					findViewById(R.id.go).setVisibility(View.GONE);
					myinfo.setText(tempBean.errMsg);
					break;
				}
			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
			}
		});
	}

	protected void setSeclectTime() {

		yajin.setText2(info.deposit_money_array.getString(bean.getId() + "") + "元");
		seclectTime.setText2(bean.name);
		time.setText(bean.name);
		money.setText(info.rentcar_money_array.getString(bean.getId() + "") + "元");
		payMoney.setText(info.rentcar_money_array.getString(bean.getId() + "") + "元");
		try {
			int renewalsdata = bean.getId();
			String validatetime = orderBean.info.long_rent_repay_date;
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date now = sdf.parse(validatetime);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(now);
			calendar.add(Calendar.MONTH, renewalsdata);
			newReturnTime.setText2(StringTools.getLongTime(sdf.format(calendar.getTime())));
		} catch (Exception e) {
			MyException.printStackTrace(e);
		}

		String str = info.usable_month_array.getString(bean.getId() + "");
		if (str != null && str.equals("0")) {
			isOnclick = false;
			pay.setBackgroundResource(R.drawable.myborder);
			findViewById(R.id.bottom).setVisibility(View.VISIBLE);
			initMoney = Double.parseDouble(info.deposit_money_array.getString(bean.getId() + ""))
					- info.car_frozen_money;
			myinfo.setText(info.disabled_content);
		} else {
			initMoney = 0;
			isOnclick = true;
			pay.setBackgroundResource(R.drawable.onclickbackground1);
			findViewById(R.id.bottom).setVisibility(View.GONE);
		}
	}

	@Override
	public void initEvent() {
		seclectTime.setOnClickListener(this);
		pay.setOnClickListener(this);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		super.onActivityResult(requestCode, resultCode, data);
		switch (resultCode) {
		case CodeConstant.ZushiCode:
			bean = (UserTimeBean) data.getSerializableExtra("bean");
			setSeclectTime();
			break;

		default:
			break;
		}

	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.seclectTime:
			if (info != null) {
				intent = new Intent(MyAppcation.getMyAppcation(), UseTimeActivity.class);
				startActivityForResult(intent, CodeConstant.ZushiCode);
			}
			break;
		case R.id.pay:
			if (isOnclick) {
				if (bean != null && info != null) {
					Get_user_money_infoRequest requestBean = new Get_user_money_infoRequest();
					promptDialog.show();
					MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

						@Override
						public void onResponse(String response) {
							promptDialog.dismiss();
							ResponseUserMoney moneyBean = JSON.parseObject(response, ResponseUserMoney.class);
							intent = new Intent(MyAppcation.getMyAppcation(), LongRentOrderPayActivity.class);
							intent.putExtra("order_id", order_id);
							intent.putExtra("tenancy_type", bean.getId());
							intent.putExtra("longRentMoney", info.rentcar_money_array.getString(bean.getId() + ""));
							intent.putExtra("moneyBean", moneyBean);
							intent.putExtra("isReRent", true);
							WXPayEntryActivity.currentStatus = WXPayEntryActivity.LONGRERENT;
							startActivity(intent);

						}
					}, new DisssmissInterFace() {

						@Override
						public void putError(VolleyError error) {
							promptDialog.dismiss();
						}
					});

				}
			}

			break;
		case R.id.go:
			intent = new Intent(MyAppcation.getMyAppcation(), PayActivity.class);
			intent.putExtra("flag", 1);
			intent.putExtra("initMoney", StringTools.getString(initMoney + ""));
			WXPayEntryActivity.currentStatus = WXPayEntryActivity.WALLET;
			startActivity(intent);
			break;

		default:
			break;
		}
	}

}
